

// let cfg = require('./utils/cfg');

class Shader{
    constructor(gl,program) {
        // this._id = cfg.genId();
        this.program = program;
        this.gl = gl;
        this._name = '';
    }

    get name(){
        return this._name;
    }

    /**
     *
     * @param newName {string}
     */
    set name(newName){
        if(this._name !== newName){
            this._name = newName;
        }
    }

    getAttribHandler(attrName){
        return this.gl.getAttribLocation(this.program,attrName);
    }

    getUniformHandler(uniforName){
        return this.gl.getUniformLocation(this.program,uniforName);
    }

    _bindHandlers(arr,bindFunc){
        if(!Array.isArray(arr)){
            throw new Error(`${arr} should is a array!`);
        }
        arr.forEach(item=>{
            if(this.hasOwnProperty(item[0])){ /* eslint-disable-line */
                throw new Error(`已经包含属性:${item[0]}`);
            }
            if(!Array.isArray(item)){
                throw new Error(`item:${item} 也应该为数组!`);
            }
            if(item.length !== 2){
                throw new Error('item.length应该为2');
            }
            this[item[0]] = bindFunc(item[1]);
        });
    }

    bindAttrs(arr){
        this._bindHandlers(arr,this.getAttribHandler.bind(this));
    }


    bindUniforms(arr){
        this._bindHandlers(arr,this.getUniformHandler.bind(this));
    }
}

module.exports = Shader;